package com.sneakxy.cloudbase.platform.utils.web;

import java.util.Collection;
import java.util.List;
import java.util.function.Function;

import org.springframework.data.domain.Page;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * datagrid数据
 * @author 潜行的青衣
 * @see
 */
@Data
@ApiModel("分页数据列表")
public class DataGrid<T> {

	@ApiModelProperty("总记录数")
	private long total;
	
	@ApiModelProperty("数据列表")
	private Collection<T> rows;
	
	public DataGrid() {}
	
	public DataGrid(Page<T> page) {
		this(page.getTotalElements(), page.getContent());
	}
	
	public <E> DataGrid(Page<E> page, Function<List<E>, List<T>> function) {
		this(page.getTotalElements(), function.apply(page.getContent()));
	}
	
	public DataGrid(Collection<T> rows) {
		this(rows.size(), rows);
	}
	
	public DataGrid(long total, Collection<T> rows) {
		this.rows = rows;
		this.total = total;
	}

	public static <T> DataGrid<T> newInstance(Page<T> page) {
		return new DataGrid<>(page);
	}
	
	public static <T> DataGrid<T> newInstance(Collection<T> rows) {
		return new DataGrid<>(rows);
	}
	
	public static <E, T> DataGrid<T> newInstance(Page<E> page, Function<List<E>, List<T>> function) {
		return new DataGrid<>(page, function);
	}
	
	public static <T> DataGrid<T> newInstance(long total, Collection<T> rows) {
		return new DataGrid<>(total, rows);
	}
	
}
